package org.jeecg.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccqtgb.sxp.partner.security.bo.Head;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.utils.ApiDataUtils;
import org.jeecg.modules.api.vo.param.CustomersCheckVo;
import org.jeecg.modules.api.vo.param.FindCustomersParamVo;
import org.jeecg.modules.api.vo.rest.CustomersCompanyBalanceVo;
import org.jeecg.modules.api.vo.rest.CustomersRestVo;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.entity.CustomerBalance;
import org.jeecg.modules.zcgl.entity.CustomerInfo;
import org.jeecg.modules.zcgl.service.ICustomerBalanceService;
import org.jeecg.modules.zcgl.service.ICustomerInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags="三峡银行接口-客户信息DEMO")
@RestController
@RequestMapping("/apiDemo/bank/customers")
@Slf4j
public class CustomersApiControllerBack {

    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private ICustomerBalanceService customerBalanceService;
    @Autowired
    private ISysDepartService sysDepartService;

    @ApiOperation(value="获取客户信息", notes="获取客户信息")
    @PostMapping("/findCustomers")
    public Result<CustomersRestVo> findCustomers(@RequestBody @Validated CustomersCheckVo vo){

        Head head = new Head();
        head.setTranchannel("ch.oth.ec.test");
        head.setUuid("2ea1e4241cfa41f58b087f2d1744b313");
        System.out.println(ApiDataUtils.getResponseData(vo, head));;

//        RequestData<PointOrderNotify> dataReslt = MessageProcess.getRequestMessageCallBack(message, "D:/log/CCQTGBtest.cer","mil1fjndiuryhjw6evi0qb a2ls93cip3", PointOrderNotify.class);
        //查询客户信息
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        CustomerInfo customerInfo = new CustomerInfo();
        BeanUtils.copyProperties(vo, customerInfo);
        queryWrapper.setEntity(customerInfo);
        CustomerInfo info = customerInfoService.getOne(queryWrapper, false);
        if (info == null){
            return Result.error("用户不存在!");
        }
        return Result.ok(getCustomersRestVo(info));
    }

    @ApiOperation(value="根据ID获取客户信息", notes="根据ID获取客户信息")
    @PostMapping("/getCustomersById")
    public Result<CustomersRestVo> findCustomers(@RequestBody @Validated FindCustomersParamVo vo){
        CustomerInfo info = customerInfoService.getById(vo.getId());
        if (info == null){
            return Result.error("用户不存在!");
        }
        return Result.ok(getCustomersRestVo(info));
    }

    private CustomersRestVo getCustomersRestVo(CustomerInfo info){
        CustomersRestVo restVo = new CustomersRestVo();
        BeanUtils.copyProperties(info, restVo);
        QueryWrapper<CustomerBalance>  balanceQueryWrapper = new QueryWrapper<>();
        balanceQueryWrapper.lambda().eq(CustomerBalance::getCustomerId, info.getId());
        List<CustomerBalance> balances = customerBalanceService.list(balanceQueryWrapper);
        List<CustomersCompanyBalanceVo> customersCompanyBalanceVoList = new ArrayList<>();
        balances.stream().forEach(balance->{
            CustomersCompanyBalanceVo customersCompanyBalanceVo = new CustomersCompanyBalanceVo();
            customersCompanyBalanceVo.setBalance(balance.getBalance());
            customersCompanyBalanceVo.setCompanyName(sysDepartService.getNameById(balance.getCompanyId()));
            customersCompanyBalanceVoList.add(customersCompanyBalanceVo);
        });
        restVo.setCustomersCompanyBalances(customersCompanyBalanceVoList);
        return restVo;
    }
}
